package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.common.enums.inOutStock.InOrderStatusEnum;
import com.canaan.business.domain.vo.ModulationInBillForPrintVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.ModulationInBillMapper;
import com.canaan.business.domain.bill.ModulationInBill;
import com.canaan.business.service.bill.IModulationInBillService;
import com.canaan.common.annotation.TableName;
/**
 * 入库调整单Service业务层处理
 *
 * @author canaan
 * @date 2025-03-24
 */
@Service
public class ModulationInBillServiceImpl implements IModulationInBillService {
    @Resource
    private ModulationInBillMapper modulationInBillMapper;

    /**
     * 查询入库调整单
     *
     * @param id 入库调整单主键
     * @return 入库调整单
     */
    @Override
    public ModulationInBill selectModulationInBillById(Long id) {
        return modulationInBillMapper.selectModulationInBillById(id);
    }
    /**
     * 查询入库调整单
     *
     * @param no 入库调整单编号
     * @return 入库调整单
     */
    @Override
    public ModulationInBill selectModulationInBillByNo(String no) {
        return modulationInBillMapper.selectModulationInBillByNo(no);
    }

    /**
     * 查询入库调整单列表
     *
     * @param modulationInBill 入库调整单
     * @return 入库调整单
     */
    @Override
    public List<ModulationInBill> selectModulationInBillList(ModulationInBill modulationInBill) {
        return modulationInBillMapper.selectModulationInBillList(modulationInBill);
    }

    /**
     * 新增入库调整单
     *
     * @param modulationInBill 入库调整单
     * @return 结果
     */
    @Override
    public int insertModulationInBill(ModulationInBill modulationInBill) {
        if(StringUtils.isEmpty(modulationInBill.getCreateBy())){
            modulationInBill.setCreateBy(SecurityUtils.getUsername());
        }
        modulationInBill.setCreateTime(DateUtils.getNowDate());
        modulationInBill.setStatus(InOrderStatusEnum.NEW.getCode());
        return modulationInBillMapper.insertModulationInBill(modulationInBill);
    }

    /**
     * 批量新增入库调整单
     *
     * @param modulationInBillList 入库调整单列表
     * @return 结果
     */
    @Override
    public int batchInsertModulationInBill(List<ModulationInBill> modulationInBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationInBill modulationInBill : modulationInBillList){
            modulationInBill.setCreateBy(username);
            modulationInBill.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<modulationInBillList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationInBillList.size()) {
                endIndex = modulationInBillList.size();
            }
            insertNum = insertNum + modulationInBillMapper.batchInsertModulationInBill(modulationInBillList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改入库调整单
     *
     * @param modulationInBill 入库调整单
     * @return 结果
     */
    @Override
    public int updateModulationInBill(ModulationInBill modulationInBill) {
        if(StringUtils.isEmpty(modulationInBill.getUpdateBy())) {
            modulationInBill.setUpdateBy(SecurityUtils.getUsername());
        }
        modulationInBill.setUpdateTime(DateUtils.getNowDate());
        return modulationInBillMapper.updateModulationInBill(modulationInBill);
    }

    /**
     * 批量修改入库调整单
     *
     * @param modulationInBillList 入库调整单列表
     * @return 结果
     */
    @Override
    public int batchUpdateModulationInBill(List<ModulationInBill> modulationInBillList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ModulationInBill modulationInBill : modulationInBillList){
            modulationInBill.setUpdateBy(username);
            modulationInBill.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<modulationInBillList.size();) {
            int endIndex = i+500;
            if (endIndex > modulationInBillList.size()) {
                endIndex = modulationInBillList.size();
            }
            updateNum = updateNum + modulationInBillMapper.batchUpdateModulationInBill(modulationInBillList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除入库调整单
     *
     * @param ids 需要删除的入库调整单主键集合
     * @return 结果
     */
    @Override
    public int deleteModulationInBillByIds(Long[] ids) {
        return modulationInBillMapper.deleteModulationInBillByIds(ids);
    }

    /**
     * 删除入库调整单信息
     *
     * @param id 入库调整单主键
     * @return 结果
     */
    @Override
    public int deleteModulationInBillById(Long id) {
        return modulationInBillMapper.deleteModulationInBillById(id);
    }

    /**
     * 订单打印
     *
     * @param no
     * @return
     */
    @Override
    public ModulationInBillForPrintVo selectModulationInBillForPrint(String no) {
        return modulationInBillMapper.selectModulationInBillForPrint(no);
    }

}
